﻿#region Using

using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using XNgine;
using XNgine.CameraManagement.Cameras;
using XNgine.Services;

#endregion

namespace XNgine_Test_Game.GameScreens
{
    public class GameplayScreen : XNgine.ScreenManagement.StandardScreens.GameplayScreen
    {
        private IKeyboardDevice _Keyboard;
        private IMouseDevice _Mouse;
        private FirstPersonCamera _Camera;

        public const string SCREEN_NAME = "TestGame.GameplayScreen";

        public GameplayScreen()
            : base(SCREEN_NAME)
        {
        }

        public override void Initialize()
        {
            _Mouse = XEngine.Instance.GetService<IMouseDevice>();
            _Keyboard = XEngine.Instance.GetService<IKeyboardDevice>();

            _Mouse.MouseMoved += mouse_MouseMoved;
            _Keyboard.KeyHeld += keyboard_KeyHeld;
            _Keyboard.KeyPressed += _Keyboard_KeyPressed;
            _Keyboard.KeyReleased += _Keyboard_KeyReleased;

            try
            {
                _Camera = (FirstPersonCamera)XEngine.Instance.GetService<ICameraManagement>().ActiveCamera;
            }
            catch { }

            Initialized = true;
        }

        void _Keyboard_KeyReleased(Collection<Keys> keys)
        {
            if (keys.Contains(Keys.Q) || keys.Contains(Keys.E))
                _Camera.StopPeeking();
        }
        void _Keyboard_KeyPressed(Collection<Keys> keys)
        {
            if (!IsInputAllowed) return;

            if (keys.Contains(Keys.Escape))
            {
                IScreenManagement service = XEngine.Instance.GetService<IScreenManagement>();
                ((MainMenu)service.Screens[MainMenu.SCREEN_NAME]).PauseMode = true;
                service.Show(MainMenu.SCREEN_NAME);
            }

            if (keys.Contains(Keys.P))
            {

            }

            if (keys.Contains(Keys.Q))
                _Camera.PeekLeft();
            if (keys.Contains(Keys.E))
                _Camera.PeekRight();
        } 
        private const float cameraMoveSpeed = 10f;
        private void keyboard_KeyHeld(Collection<Keys> keys)
        {
            if (!IsInputAllowed) return;
            if (_Camera == null) return;

            float time = (float)XEngine.Instance.GameTime.ElapsedGameTime.TotalSeconds;

            if (keys.Contains(Keys.W))
                _Camera.Translate(_Camera.Forward * (cameraMoveSpeed * time));
            if (keys.Contains(Keys.S))
                _Camera.Translate(_Camera.Backward * (cameraMoveSpeed * time));
            if (keys.Contains(Keys.A))
                _Camera.Translate(_Camera.Left * (cameraMoveSpeed * time));
            if (keys.Contains(Keys.D))
                _Camera.Translate(_Camera.Right * (cameraMoveSpeed * time));
        }

        private const float cameraSpeed = 0.001f;
        private void mouse_MouseMoved(Vector2 move)
        {
            if (!IsInputAllowed) return;
            if (_Camera == null) return;

            _Camera.Rotate(cameraSpeed * move.X, cameraSpeed * move.Y);
        }

        public override void Draw()
        {
            IRenderManagement renderer = XEngine.Instance.GetService<IRenderManagement>();
            renderer.RenderObjects();
        }

        public override void Update()
        {
        }
    }
}